package com.wanho.java159.code.gen.core;

import com.wanho.java159.code.gen.util.Common;
import com.wanho.java159.code.gen.util.FileUtil;
import com.wanho.java159.code.gen.util.GenCodeType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhang
 * @version 1.0
 * @date 2021/7/10
 */
public class VueGen {
    public static void genVueCode(Map<String, Object> entityData) throws Exception{
        System.out.println("===========生成 vue 层  开始======================");
        //生成 VUE API
        genVueAPI(entityData) ;
        //生成 List 页面
        genVueList(entityData) ;
        //生成 Form 页面
        genVueForm(entityData) ;
        //生成 router
        genVueRouter(entityData) ;
        System.out.println("===========生成 vue 层  结束======================");
    }

    private static void genVueAPI(Map<String, Object> entityData) throws Exception{
        FileUtil.generateToJava(GenCodeType.VUE_API, entityData);
    }

    private static void genVueList(Map<String, Object> entityData) throws Exception{
        fillPoProperty(entityData) ;
        FileUtil.generateToJava(GenCodeType.VUE_LIST, entityData);
    }

    private static void genVueForm(Map<String, Object> entityData) throws Exception{
        FileUtil.generateToJava(GenCodeType.VUE_FORM, entityData);
    }

    private static void genVueRouter(Map<String, Object> entityData) throws Exception{
        FileUtil.generateToJava(GenCodeType.VUE_ROUTER, entityData);
    }

    private static void fillPoProperty(Map<String, Object> entityData) throws Exception{
        //获得 实体类简单名称
        String entity = (String) entityData.get("entity");
        //通过IO读取文件中 属性 排除 id ,createBy,createTime,updateTime,isDeleted公共字段
        File file = new File(Common.PO_DIR_PATH, entity + ".java");
        List<String> list = FileUtils.readLines(file, "UTF-8");
        List<String> propList = new ArrayList<>() ;
        String[] exclueLineAy = {
                "private Long id;",
                "private Long createBy;",
                "private Date createTime;",
                "private Date updateTime;",
                "private Boolean isDeleted;",
                "private String remark;"
        } ;
        for (String line : list) {
            line = line.trim() ;
            //排除；结尾
            if (!line.endsWith(";")){
                continue;
            }
            //排除 import 和 package语句
            if (line.startsWith("import") || line.startsWith("package") || line.startsWith("this.") || line.startsWith("return") || line.startsWith("public")){
                continue;
            }
            //去掉首尾空格
            if (!ArrayUtils.contains(exclueLineAy,line)){
                String[] ay = line.split(" ");
                String last = ay[ay.length-1];
                String propName = last.substring(0,last.length()-1) ;
                propList.add(propName) ;
            }
        }
        entityData.put("propList",propList) ;
    }
}
